package com.zw.pdm.module.basic.service;

import com.zw.pdm.constant.Constant;
import com.zw.pdm.entity.basic.*;
import com.zw.pdm.entity.basic.Process;
import com.zw.pdm.module.basic.mapper.DictCommonMapper;
import com.zw.pdm.module.basic.repository.DictCommonRepository;
import com.zw.pdm.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static com.zw.pdm.constant.Constant.STATUS_NORMAL;
import static com.zw.pdm.util.BeanUtil.copy;


/**
 * @Description:
 *
 * @Author: China.sgl
 * @Date: 2019/12/6 08:42
 */
@Service
@SuppressWarnings("all")
public class DictCommonService {

    @Autowired
    private DictCommonMapper dictCommonMapper;
    @Autowired
    private DictCommonRepository dictCommonRepository;

    /** 按条件查询字典数据 */
    public List<DictCommon> findDictCommonByCondition(DictCommon dict) {
        return dictCommonMapper.findByCondition(dict);
    }

    /** 保存字典数据信息 */
    public DictCommon saveDictCommon(DictCommon dict) {
        dict.setStatus(STATUS_NORMAL);
        dict.setCreateTime(new Date());
        return dictCommonRepository.save(dict);
    }

    /** 修改字典数据 */
    public DictCommon updateDictCommon(DictCommon dict) {
        DictCommon dictCommon = dictCommonRepository.getOne(dict.getId());
        copy(dict, dictCommon);
        return dictCommonRepository.saveAndFlush(dictCommon);
    }

    /** 删除字典数据 */
    public int delDictCommon(Integer id) {
        return dictCommonMapper.updateStatusById(id, Constant.STATUS_DELETE);
    }

    /** 按分类查询字典数据 */
    public List<DictCommon> findDictByGroupName(String groupName) {
        return dictCommonMapper.findByGroupName(groupName);
    }

    /**按提报计划部门分类查询字典表类型*/
    public List<DictCommon> findByGroupNameDept(String groupName){
        return dictCommonMapper.findByGroupNameDept(groupName);
    }

    /** 查询路线 */
    public List<Route> findRoutes() {
        return dictCommonMapper.findRoutes();
    }

    /** 查询工序 */
    public List<Process> findProcesses() {
        return dictCommonMapper.findProcesses();
    }

    /** 查询设备 */
    public List<Equ> findEqu() {
        return dictCommonMapper.findEqu();
    }

    /** 按类型查人员 */
    public List<Person> findPersonByCategory(String category) {
        Integer branchId = ShiroUtils.getUser().getDeptId();
        return dictCommonMapper.findPersonByCategory(category, branchId);
    }
}
